//
//  LanguageChangeViewController.m
//  CBT
//
//  Created by Mr.Zhang on 2018/11/19.
//  Copyright © 2018 Mr.Zhang. All rights reserved.
//

#import "LanguageChangeViewController.h"
#import "LanguageChangeCell.h"

@interface LanguageChangeViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIImageView *selectImageView;
@property (nonatomic, assign) NSInteger selectNum;

@end

@implementation LanguageChangeViewController

#pragma mark - VC Life

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.selectNum = [self getAppLabgyage];
    
    self.navigationItem.title = kLocalizedString(@"语言界面", nil);
    [self.view addSubview:self.tableView];
}

//- (NSInteger)getAppLabgyage
//{
//    if ([kLanguageManager isSimplified]) {
//        return 2;
//    }else if ([kLanguageManager isKO]){
//        return 1;
//    }else{
//        return 0;
//    }
//}
//
//- (NSString *)setAppLabgyageWithIndexPathRow:(NSInteger)indexRow
//{
//    if (indexRow == 0) {
//        return @"en-CN";
//    }else if (indexRow == 1) {
//        return @"ko";
//    }else{
//        return @"zh-Hans-CN";
//    }
//}

- (NSInteger)getAppLabgyage
{
    if ([kLanguageManager isSimplified]) {
        return 1;
    }
    else {
        return 0;
    }
}

- (NSString *)setAppLabgyageWithIndexPathRow:(NSInteger)indexRow
{
    if (indexRow == 0) {
        return @"en-CN";
    }
    else {
        return @"zh-Hans-CN";
    }
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LanguageChangeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"changeCell"];
    cell.selectLabel.text = indexPath.row == 0 ? @"English" : @"简体中文";
    if (self.selectNum == indexPath.row) {
        [cell.contentView addSubview:self.selectImageView];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [kLanguageManager setUserlanguage:[self setAppLabgyageWithIndexPathRow:indexPath.row]];
    self.navigationItem.title = kLocalizedString(@"语言界面", nil);
    self.selectNum = indexPath.row;
    [self.tableView reloadData];
    
   // [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeLanguage" object:nil];
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

#pragma mark - Lazy

- (UIImageView *)selectImageView
{
    if (!_selectImageView) {
        _selectImageView = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenNarrowSide - 32, 16.5, 17, 17)];
        _selectImageView.image = [UIImage imageNamed:@"select"];
    }
    return _selectImageView;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, ScreenNarrowSide, ScreenWideSide - self.tabBarController.tabBar.bounds.size.height - 64) style:(UITableViewStylePlain)];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellEditingStyleNone;
        
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        [_tableView registerClass:[LanguageChangeCell class] forCellReuseIdentifier:@"changeCell"];
        
        if (@available(iOS 11.0, *)){
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }else{
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        _tableView.estimatedRowHeight           = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

@end
